import {
  Component
} from '@angular/core';
import {
  NavController,
  NavParams,
  ModalController,
  LoadingController,
  ViewController, Events,
} from 'ionic-angular';
import {
  InformationOfOthersPage
} from '../information-of-others/information-of-others';
import {
  ChangePurposePage
} from '../change-purpose/change-purpose';
import {
  Http,
  Headers,
  RequestOptions
} from '@angular/http';
import {
  baseCommon
} from '../../common/baseCommon';
import {
  AppConfig
} from '../../app/app.config';
import {
  Storage
} from '@ionic/storage'

/**
 * Generated class for the AroundPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-around',
  templateUrl: 'around.html',
})
export class AroundPage{

  salary: any;
  city: any;
  listData = [];
  myPurpose = [];
  phone: any;
  num: number = 0;
  ifSuccess: boolean = false;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public modalCtrl: ModalController,
              public http: Http,
              public storage: Storage,
              public event: Events,
              public viewCtrl: ViewController,
              public loadingCtrl: LoadingController) {
  }

  ionViewDidLoad() {
    this.storage.get('sessionID').then(val => {
      this.getNearuser(AppConfig.httpLoc + '/nearuser?sessionID=' + val);
    })

    this.storage.get('phone').then(val => {
      this.phone = val;
    })

    this.storage.get('sessionID').then(val => {
      baseCommon.getInfo(this.http,this.phone,val)
        .then(val=>{
          this.myPurpose = val.targets;
          this.ifSuccess = true;
        });
    })
  }

  /**
   * 下拉刷新
   *
   * @param {any} refresher
   * @memberof AroundPage
   */
  doRefresh(refresher) {
    this.storage.get('sessionID').then(val => {
      this.getNearuserRef(AppConfig.httpLoc + '/nearuser?sessionID=' + val, refresher);
    })
  }

  /**
   * 进入到他人信息页面
   *
   * @param {any} phone
   * @memberof AroundPage
   */
  viewOtherData(phone,name) {
    let model = this.modalCtrl.create(InformationOfOthersPage, {'phone': phone,'name':name});
    model.onDidDismiss(data => {
      AppConfig.viewCtrl = null;
    })
    model.present();
  }

  /**
   * 进入到更改目的页面
   *
   * @memberof AroundPage
   */
  changePurpose() {
    let model = this.modalCtrl.create(ChangePurposePage, {'myPurpose': this.myPurpose});
    model.onDidDismiss(data => {
      this.storage.get('phone').then(val => {
        this.phone = val;
      })

      this.storage.get('sessionID').then(val => {
        baseCommon.getInfo(this.http,this.phone,val)
        .then(val=>{
          this.myPurpose = val.targets;
          this.ifSuccess = true;
        });
      })
      AppConfig.viewCtrl = null;
    });
    model.present();
  }

  /**
   * 得到附近的人
   *
   * @param {any} apiUrl
   * @memberof AroundPage
   */
  getNearuser(apiUrl) {

    let loading = baseCommon.showLoading(this.loadingCtrl, '寻找中....')

    let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    let options = new RequestOptions({
      headers: headers,
    });

    this.http.get(apiUrl, options)
      .toPromise()
      .then(data => {
        this.listData = data.json().data;
        loading.dismissAll();
        this.ifSuccess = true;
      }, err => {
        console.log(err);
      })
  }

  /**
   * 下拉刷新请求附近人
   *
   * @param {any} apiUrl
   * @param {any} refresher
   * @memberof AroundPage
   */
  getNearuserRef(apiUrl, refresher) {

    let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    let options = new RequestOptions({
      headers: headers,
    });

    this.http.get(apiUrl, options)
      .toPromise()
      .then(data => {
        setTimeout(() => {
          this.listData = data.json().data;
          refresher.complete();
        }, 1000);
      }, err => {
        console.log(err);
      })
  }

}
